﻿using System;
using System.Linq;
using System.Net.Sockets;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using BrainTechLLC;
using BrainTechLLC.ThreadSafeObjects;
using System.Threading;
using System.Runtime.Serialization;
using System.IO;
using System.Xml;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace BrainTechLLC.Silverlight.Controls
{
    public class BaseDataVisualizer : UserControl
    {
        public event EventHandler<ObjectEventArgsNonRef<long>> RequestEditID;
        public event EventHandler<ObjectEventArgs<LocationInt>> RequestGoLocation;
        public event EventHandler<ObjectEventArgs<LocationInt>> RequestEditGameLocation;

        public event EventHandler<ObjectEventArgsNonRef<bool>> WantClose;
        public event EventHandler CloseWindow;
        public event EventHandler<ObjectEventArgsNonRef<bool>> RequestSave;
        public event EventHandler Save;

        public virtual Panel MainCanvas { get { return Content as Panel; } }
        public WindowContainer Window { get { return this.FindParent<WindowContainer>(); } }

        public bool IsOpen
        {
            get { return (bool)GetValue(WindowContainer.IsOpenProperty); }
            set { SetValue(WindowContainer.IsOpenProperty, value); }
        }

        public bool ShowClose
        {
            get { return (bool)GetValue(WindowContainer.ShowCloseProperty); }
            set { SetValue(WindowContainer.ShowCloseProperty, value); }
        }

        public bool ShowSave
        {
            get { return (bool)GetValue(WindowContainer.ShowSaveProperty); }
            set { SetValue(WindowContainer.ShowSaveProperty, value); }
        }

        public bool ShowEdit
        {
            get { return (bool)GetValue(WindowContainer.ShowEditProperty); }
            set { SetValue(WindowContainer.ShowEditProperty, value); }
        }

        public DataVisualizer DataVisualizer
        {
            get { return this.FindParent<DataVisualizer>(); }
        }

        public virtual void UpdateBindings()
        {
        }

        public virtual string TitleText { get { return string.Empty; } }

        protected string _uniqueString;
        public virtual string UniqueString
        {
            get
            {
                if (string.IsNullOrEmpty(_uniqueString))
                    _uniqueString = this.GetType().Name;

                return _uniqueString;
            }
            set
            {
                _uniqueString = value;
            }
        }

        public virtual void SetVisualizedData(DataVisualizer vis, object o)
        {
            string text = TitleText;

            vis.UniqueString = UniqueString;

            if (!string.IsNullOrEmpty(text))
                vis.DescriptionTextString = text;
            
            this.ShowSave = vis.ShowSave;
        }

        public void Refresh()
        {
            if (Window != null)
            {
                Window.IsOpen = (bool)GetValue(WindowContainer.IsOpenProperty);
                Window.ShowClose = (bool)GetValue(WindowContainer.ShowCloseProperty);
                Window.ShowEdit = (bool)GetValue(WindowContainer.ShowEditProperty);
                Window.ShowSave = (bool)GetValue(WindowContainer.ShowSaveProperty);
                Window.UniqueString = UniqueString;
            }

            UpdateBindings();
        }

        public BaseDataVisualizer()
        {
            this.Loaded += new RoutedEventHandler(BaseDataVisualizer_Loaded);
        }

        void BaseDataVisualizer_Loaded(object sender, RoutedEventArgs e)
        {
            if (Window != null)
            {
                Window.CloseWindow += new EventHandler(Window_CloseWindow);
                Window.WantClose += new EventHandler<ObjectEventArgsNonRef<bool>>(Window_WantClose);
                Window.Save += new EventHandler(Window_Save);
                Window.RequestSave += new EventHandler<ObjectEventArgsNonRef<bool>>(Window_RequestSave);
                Window.UniqueString = UniqueString;
            }

            Refresh();
        }

        void Window_RequestSave(object sender, ObjectEventArgsNonRef<bool> e)
        {
            if (RequestSave != null)
                RequestSave(sender, e);
        }

        void Window_Save(object sender, EventArgs e)
        {
            if (Save != null)
                Save(sender, e);

            OnSaveData();
        }

        public virtual void OnSaveData()
        {
        }

        void Window_WantClose(object sender, ObjectEventArgsNonRef<bool> e)
        {
            if (WantClose != null)
                WantClose(sender, e);
        }

        void Window_CloseWindow(object sender, EventArgs e)
        {
            if (CloseWindow != null)
                CloseWindow(sender, e);
        }

        public void OnRequestEditID(object sender, ObjectEventArgsNonRef<long> e)
        {
            if (RequestEditID != null)
                RequestEditID(sender, e);
        }

        public void OnRequestGoToLocation(object sender, ObjectEventArgs<LocationInt> e)
        {
            if (RequestGoLocation != null)
                RequestGoLocation(this, e);
        }

        public void OnRequestEditGameLocation(object sender, ObjectEventArgs<LocationInt> e)
        {
            if (RequestEditGameLocation != null)
                RequestEditGameLocation(this, e);
        }
    }
}
